import * as Router from 'koa-router'
import * as Joi from 'joi'
import * as AdminrService from '../services/admin.service'
import * as userService from '../services/user.service'
import { badParams, stats } from '../stats'
import { Identity } from '../types'

const router = new Router({
  prefix: '/api/admin'
})

/* 管理员api */

// 登录 ok √
router.post('/login', async ctx => {
  const schema = Joi.object({
    username: Joi.string().min(2).max(10).required(),
    password: Joi.string().min(6).max(18).required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const user = await ctx.state.userInfo
  if (user) throw stats.ERR_REPEAT
  try {
    const { token, user } = await userService.login(value)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    ctx.set('Set-Cookie', `token=${token}; path=/; httpOnly`)
    ctx.body = {
      stat: 'OK',
      user
    }
  } catch (error) {
    ctx.body = error
  }
})

// 审核列表
router.post('/judge/list', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const data = await AdminrService.judgelist(value)
    ctx.body = {
      stat: 'OK',
      data
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 书籍列表
router.post('/books', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required(),
    search: Joi.string()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const data = await AdminrService.books(value)
    ctx.body = {
      stat: 'OK',
      data
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 书籍详情
router.post('/book/detail', async ctx => {
  const schema = Joi.object({
    bookId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const data = await AdminrService.detail(value.bookId)
    ctx.body = {
      stat: 'OK',
      data
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 评论列表
router.post('/remarks', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const data = await AdminrService.remarks(value)
    ctx.body = {
      stat: 'OK',
      data
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 用户列表
router.post('/users', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required(),
    search: Joi.string()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const data = await AdminrService.users(value)
    ctx.body = {
      stat: 'OK',
      data
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 审核书籍
router.post('/judge', async ctx => {
  const schema = Joi.object({
    bookId: Joi.string().required(),
    putaway: Joi.boolean().required(),
    content: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const res = await AdminrService.launch(user._id, value)
    if (res) {
      ctx.body = {
        stat: 'OK'
      }
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 处理评论
router.post('/deal/remark', async ctx => {
  const schema = Joi.object({
    remarkId: Joi.string().required(),
    flag: Joi.boolean().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    const res = await AdminrService.control(value.remarkId, value.flag)
    if (res) {
      ctx.body = {
        stat: 'OK'
      }
    }
  } else throw stats.ERR_NOT_LOGIN
})

router.post('/deal/user', async ctx => {
  const schema = Joi.object({
    userId: Joi.string().required(),
    role: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await userService.userInfo(token)
    if (user.role !== Identity.Admin) throw stats.ERR_NOT_RIGHT
    if (user._id.equals(value.userId)) throw stats.ERR_FORBIDDEN
    const res = await AdminrService.dealuser(value.userId, value.role)
    if (res) {
      ctx.body = {
        stat: 'OK'
      }
    }
  } else throw stats.ERR_NOT_LOGIN
})

export default router
